package com.ruoyi;

import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.text.DateFormat;
import java.util.Base64;

public class LicenceCreator {

    /**
     * 使用 Base64 编码的公钥字符串加密明文
     *
     * @param publicKeyStr Base64编码的公钥字符串
     * @param plainText    待加密的明文
     * @return Base64编码的加密后密文
     * @throws Exception
     */
    public static String encrypt(String publicKeyStr, String plainText) throws Exception {
        // 将公钥字符串解码为字节数组
        byte[] keyBytes = Base64.getDecoder().decode(publicKeyStr);
        // 生成公钥对象（X509EncodedKeySpec）
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(keySpec);

        // 获取Cipher实例，初始化为加密模式
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));

        // 返回 Base64 编码的密文
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    /**
     * 使用 Base64 编码的私钥字符串解密密文
     *
     * @param privateKeyStr Base64编码的私钥字符串
     * @param cipherText    Base64编码的密文
     * @return 解密后的明文
     * @throws Exception
     */
    public static String decrypt(String privateKeyStr, String cipherText) throws Exception {
        // 将私钥字符串解码为字节数组
        byte[] keyBytes = Base64.getDecoder().decode(privateKeyStr);
        // 生成私钥对象（PKCS8EncodedKeySpec）
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

        // 获取Cipher实例，初始化为解密模式
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(cipherText));

        return new String(decryptedBytes, "UTF-8");
    }
    public static void main(String[] args) {
        try {
            // 示例：先生成密钥对（实际中密钥可能通过其它方式获取或存储）
//            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
//            keyPairGenerator.initialize(2048);
//            java.security.KeyPair keyPair = keyPairGenerator.generateKeyPair();
//            String publicKeyStr = Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded());
//            String privateKeyStr = Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded());
//
//            System.out.println("公钥：" + publicKeyStr);
//            System.out.println("私钥：" + privateKeyStr);
            // 加密示例
            String plainText = "2025-11-01 00:00:00";
            String cipherText = encrypt("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAi0fOSTtKGVBYr/gysUBX2l5dmK4POJc/9h0yK35sw/F/zAfcr8atF6sKyZolsGo+Wl4mTgOE6Q4maKorN7UxJrL2kH/V2F8buVgA3MKeahmRdHkDBjrcfRimRHvlrmWiRZKBK/a710e8uHPMwmu17LB4wfmzq8P0w8Hn8miv/OHligrpTfqxGgacRnWUwrGLmrgeuqqPvBrbUCPyjtgPDUlA0kPNCYmk/AE6+ZeIohXsxGx1NCR9dyoBMItWyybcNDEOWvJssz5Ec/IrVcI+fiPvbcta3LAoT3PuJdy22masrt7kxfi3XUgRe2sQq7zclEsGzUjsJ6cO0/77/5cREwIDAQAB", plainText);
            System.out.println("加密后的密文：" + cipherText);
            // 解密示例
            String decryptedText = decrypt("MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCLR85JO0oZUFiv+DKxQFfaXl2Yrg84lz/2HTIrfmzD8X/MB9yvxq0XqwrJmiWwaj5aXiZOA4TpDiZoqis3tTEmsvaQf9XYXxu5WADcwp5qGZF0eQMGOtx9GKZEe+WuZaJFkoEr9rvXR7y4c8zCa7XssHjB+bOrw/TDwefyaK/84eWKCulN+rEaBpxGdZTCsYuauB66qo+8GttQI/KO2A8NSUDSQ80JiaT8ATr5l4iiFezEbHU0JH13KgEwi1bL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zq3vWW1+jF010lGxyaad8exdPGafiBhRzYDZUFw4sXeTsWy4/vmmr2uNjJ+yar7J5QKBgAqO+JFD/YWbRJpPzV1PosZgUDmC9SUHOpzH0ggHk4ZORPDXCwzkyVGsN70j5c3m1sVW49H717ueOwhyVZF2WIq/S8BxFvb+J+aCkCzDaapXiwBfkXyRB9DmbYgqHDK8vQKIUZFxLFaDXQX29etPrLlksaPxr4MKEODxJIeGppd5", cipherText);
            System.out.println("解密后的明文：" + decryptedText);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
